/**
 *    @file       servertask.h
 *
 *    @brief      
 *    @details
 *
 *    @version    1.0
 *    @date       02/19/2014 09:37:23 PM
 *
 *    @author     Theophilus (), wangtf418@gmail.com 
 */
#ifndef _SF_SERVERTASK_H_
#define _SF_SERVERTASK_H_
#include "sockettask.h"

class servertask : public sockettask
{
public:
	servertask(csocket *sock, netprocessthread *netthread, const comargs *args = NULL);
	virtual ~servertask();
	enum read_state_t
	{
		READ_HEAD = 0,
		READ_BODY = 1
	};

public:
	virtual bf_int_t init(bf_time_t timeout = SOCKTASK_TIMEOUT, bf_int_t sock_buflen = 102400);
	virtual bf_int_t proc_msg(message *msg);
	virtual bf_int_t process_io_event(bf_int_t events);

protected:
	virtual bf_int_t recv_process();
	virtual bf_int_t send_process();

protected:
	virtual bf_int_t read_head();
	virtual bf_int_t read_body();
	virtual bf_int_t process_recv_packet(bf_char_t *buf, bf_size_t buflen);
	bf_int_t send_packet(send_message *cur_sendmsg);
	virtual void reset_buf() = 0;
	virtual bf_size_t get_bodylen(const bf_char_t *buf, bf_size_t buflen) = 0;
	virtual bf_uint_t get_cmdid(const bf_char_t *buf, bf_size_t buflen) = 0;

protected:
	bf_char_t m_recvbuf[TCP_RECV_BUF_LEN];
	read_state_t m_read_state;
	bf_size_t m_cur_recvlen;
	bf_size_t m_headlen;
	bf_size_t m_bodylen;

protected:
	bf_bool_t m_close_flag;
};

#endif

